<template>
    <div class="container" :style="'min-height: '+docheight+'px'">
      <div v-if="!showTimeOut&&showCont">
        <div class="goodsItem" v-if="orderInfo.course">
            <div class="goodsImg">
                <img :src="orderInfo.course.course_cover+ossSrc">
            </div>
            <div class="goodsInfo">
                <div class="goodsName">{{ orderInfo.course.course_name }}</div>
                <div class="goodsType">
                    <div class="goodsTypeItem">已选：{{ orderInfo.course.num+','+$route.query.course_rule_name }}</div>
                </div>
                <div class="goodsInfoPrice">
                    <div>单价：¥{{ orderInfo.course.course_price }}</div>
                </div>
            </div>
        </div>
        <div class="formCont">
            <div class="formHead">
                <div>确认下单</div>
            </div>
            <div class="formList">
                <div class="formItem">
                    <van-field maxlength="20" label="服务城市" v-model="cityVal" disabled />
                </div>
                <div class="formItem">
                    <van-field maxlength="20" label="服务地址" v-model="info.service_address" placeholder="XX县(区)XX小区" />
                </div>
                <div class="formItem">
                    <van-field maxlength="20" label="门牌号" v-model="info.house_number" placeholder="详细地址，例：X栋X单元XX号" />
                </div>
                <div class="formItem">
                    <van-field maxlength="10" label="联系姓名" v-model="info.user_name" placeholder="请输入联系姓名" />
                </div>
                <div class="formItem">
                    <van-field maxlength="11" label="联系电话" v-model="info.user_phone" placeholder="方便服务人员上门联系" />
                </div>
                <div class="formItem">
                    <van-field name="radio" label="性别要求">
                        <template #input>
                            <van-radio-group checked-color="#6455d4" v-model="info.teacher_sex" direction="horizontal">
                                <van-radio :name="0">不限</van-radio>
                                <van-radio :name="1">男</van-radio>
                                <van-radio :name="2">女</van-radio>
                            </van-radio-group>
                        </template>
                    </van-field>
                </div>
                <div class="formItem">
                    <van-field
                        v-model="info.sk_date"
                        is-link
                        readonly
                        label="授课日期"
                        placeholder="请选择授课日期"
                        @click="showDayMask = true"
                    />
                    <van-popup v-model:show="showDayMask" round position="bottom">
                        <div class="maskCont">
                            <div class="maskName">*请选择授课日期</div>
                            <div class="timeCont">
                                <div :class="index%4==0?'timeItem noLeft':'timeItem'" :key="index" v-for="(item,index) in dayArr">
                                    <div @click="checkDayFn(item,index)" :class="item.active?'activeTimeMain timeItemMain':'timeItemMain'">
                                        <span>{{ item.name }}</span>
                                    </div>
                                </div>
                            </div>
                            <div class="setBtn">
                                <save-btn color="#6455d4" value="确认选择" style="width: 100%;" @saveClick="sureCheckDay"></save-btn>
                            </div>
                        </div>
                    </van-popup>
                </div>
                <div class="formItem">
                    <van-field
                        v-model="info.sk_time"
                        is-link
                        readonly
                        label="授课时间"
                        placeholder="请选择授课时间"
                        @click="showTime = true"
                    />
                    <van-popup v-model:show="showTime" round position="bottom">
                        <div class="maskCont">
                            <div class="maskName">*选择教师授课时间范围</div>
                            <div class="timeCont">
                                <div :class="index%4==0?'timeItem noLeft':'timeItem'" :key="index" v-for="(item,index) in timeArr">
                                    <div @click="timeFn(item,index)" :class="item.active?'activeTimeMain timeItemMain':'timeItemMain'">
                                        <span>{{ item.time }}</span>
                                    </div>
                                </div>
                            </div>
                            <div class="setBtn">
                                <div class="setBtnItem">
                                    <save-btn color="#6455d4" value="全天授课" style="width: 100%;" @saveClick="allDayFn"></save-btn>
                                </div>
                                <div class="setBtnItem">
                                    <save-btn color="#6455d4" value="确认时间" v-if="startIndex>=0&&endIndex>0&&endIndex>startIndex" style="width: 100%;" @saveClick="sureCheckTime"></save-btn>
                                    <save-btn color="#6455d4" value="请选择时间" v-else style="width: 100%;opacity: 0.5;"></save-btn>
                                </div>
                            </div>
                        </div>
                    </van-popup>
                </div>
                <div class="formItem">
                    <van-field maxlength="6" label="邀请码" v-model="info.user_code" placeholder="没有邀请码可不填" />
                </div>
                <div class="formItem noBorder">
                    <van-field
                        rows="4"
                        label="服务备注"
                        autosize
                        v-model="info.service_remark"
                        type="textarea"
                        maxlength="150"
                        placeholder="若有其他需求，请备注。（例如：可提前来，请尽快联系我，来之前打电话）"
                        show-word-limit
                    />
                </div>
            </div>
        </div>
        <div class="ctrlCont">
            <div class="leftBtn">
                待支付：{{orderInfo.order_total_price}}元
            </div>
            <div class="rightBtn" @click="sureFn">立即购买</div>
        </div>
      </div>
      <van-popup v-model:show="showConfirm" round position="bottom">
        <div class="orderMask">
            <div class="maskTitle">下单提示</div>
            <div class="maskTip">
                <p>平台只接受"四川省眉山市仁寿县城区内"上门家教授课，超出范围请勿下单！</p>
                <p>下单之后平台会致电联系，确认无误之后会通知教师将在24小时内接单，若超出接单时间，金额原路退回到用户账户，请放心预约。</p>
            </div>
            <saveBtn color="#6455d4" style="width: 100%;margin-top: 0.75rem;" value="确认下单" @saveClick="sureMask"></saveBtn>
        </div>
      </van-popup>
      <toast v-model="showToast" position="middle" width="50%" type="text" :time="1000">{{showText}}</toast>
      <getLoad v-if="!showCont"></getLoad>
      <sub-load v-if="showLoad"></sub-load>
      <getTimeOut v-if="showTimeOut"></getTimeOut>
    </div>
  </template>
  
  <script>
  
  import {orderSub,orderSave} from "@/http/api";
  import WeixinJSBridge from 'weixin-js-sdk'
  import getLoad from '@/components/public/getLoad'
    import getTimeOut from '@/components/public/getTimeOut'
    import saveBtn from '@/components/public/saveBtn.vue'
    import { Toast } from 'vux'
    import subLoad from '@/components/public/subLoad.vue'

    export default {
      components: {subLoad,Toast,getLoad,getTimeOut,saveBtn},
      data() {
        return {
          showTimeOut:false,
          showCont:false,
          showText:'',
          showToast:false,
          info:{
            family_course_id:this.$route.query.family_course_id,
            service_address:'',
            house_number:'',
            user_name:'',
            user_phone:'',
            sk_time:'',
            teacher_sex:0,
            user_code:'',
            num:this.$route.query.num,
            sk_date:'',
            course_price:this.$route.query.course_price,
            course_rule_name:this.$route.query.course_rule_name,
            service_remark:'',
          },
          cityVal:'四川省眉山市仁寿县',
          timeArr:[],
          activeDate:'',
          activeTime:'',
          orderInfo:{},
          showLoad:false,
          showConfirm:false,
          showTime:false,
          showDayMask:false,
          dayArr:[
            {name:'周一',active:false},
            {name:'周二',active:false},
            {name:'周三',active:false},   
            {name:'周四',active:false},
            {name:'周五',active:false},
            {name:'周六',active:false},
            {name:'周天',active:false},
          ],
            timeArr:[
                {time:'08:00',active:false},
                {time:'09:00',active:false},
                {time:'10:00',active:false},
                {time:'11:00',active:false},
                {time:'12:00',active:false},
                {time:'13:00',active:false},
                {time:'14:00',active:false},
                {time:'15:00',active:false},
                {time:'16:00',active:false},
                {time:'17:00',active:false},
                {time:'18:00',active:false},
                {time:'19:00',active:false},
                {time:'20:00',active:false},
                {time:'21:00',active:false},
                {time:'22:00',active:false},
            ],
            startIndex:'',
            endIndex:'',
            startTime:"",
            endTime:"",
        }
      },
      props: {},
      methods:{
        checkDayStr(){
            let arr = []
            for(let i = 0;i<this.dayArr.length;i++){
                if(this.dayArr[i].active){
                    arr.push(this.dayArr[i].name)
                }
            }
            return arr.join(",")
        },
        sureMask(){
            let data = this.info
            data.sk_date = this.checkDayStr()
            if(data.service_address.length<=2){
                this.showToast = true
                this.showText = '请输入服务地址'
                return false
            }else if(data.house_number.length<=2){
                this.showToast = true
                this.showText = '请输入门牌号'
                return false
            }else if(data.user_name.length==''){
                this.showToast = true
                this.showText = '请输入联系姓名'
                return false
            }else if(!this.$converData.checkPhone(data.user_phone)){
                this.showToast = true
                this.showText = '请输入正确电话'
                return false
            }else if(!data.sk_time){
                this.showToast = true
                this.showText = '请选择授课时间'
                return false
            }else if(!data.sk_date){
                this.showToast = true
                this.showText = '请选择授课日期'
                return false
            }else if(data.user_code!=''&&data.user_code.length!=6){
                this.showToast = true
                this.showText = '请填写正确邀请码'
                return false
            }
            if(!this.showLoad){
                this.showLoad = true
                orderSave(data).then((res)=>{
                    if(res.data.code==1){
                        WeixinJSBridge.invoke('getBrandWCPayRequest', {
                            appId: res.data.data.appId,   //公众号ID，由商户传入    
                            timeStamp: res.data.data.timeStamp,   //时间戳，自1970年以来的秒数    
                            nonceStr: res.data.data.nonceStr,      //随机串    
                            package: res.data.data.package,
                            signType: res.data.data.signType,     //微信签名方式：    
                            paySign: res.data.data.paySign, //微信签名
                        },
                        (resData)=>{
                            if (resData.err_msg == "get_brand_wcpay_request:ok") {
                                // 使用以上方式判断前端返回,微信团队郑重提示：
                                //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                                this.showToast = true
                                this.showText = '支付成功'
                                setTimeout(()=>{
                                    this.$router.replace({path:'/order'})
                                },1000)
                            }else{
                                this.showToast = true
                                this.showText = '支付失败'
                            }
                        })
                    }else{
                        this.showToast = true
                        this.showText = res.data.msg
                    }
                this.showLoad = false
                }).catch((err)=>{
                    console.log(err)
                    this.showLoad = false
                    this.showToast = true
                    this.showText = '网络请求超时'
                })
            }
        },
        checkDayFn(item,index){
            item.active= !item.active
        },
        sureCheckTime(){
            if(this.startIndex>=0&&this.endIndex>0&&this.endIndex>this.startIndex){
                this.info.sk_time = this.timeArr[this.startIndex].time+'-'+this.timeArr[this.endIndex].time
                this.showTime = false;
                this.startTime = this.timeArr[this.startIndex].time
                this.endTime = this.timeArr[this.endIndex].time
            }
        },
        allDayFn(){
            for(let i = 0;i<this.timeArr.length;i++){
                this.timeArr[i].active = true
            }
            this.startIndex = 0
            this.endIndex = this.timeArr.length-1
            this.sureCheckTime()
        },
        timeFn(item,index){
            if(this.startIndex.length==0||(this.startIndex==index&&this.startIndex!=0)){
                this.startIndex = index
                item.active  = true
            }else if(this.startIndex.length!=0&&this.endIndex.length!=0){
                for(let i = 0;i<this.timeArr.length;i++){
                    this.timeArr[i].active = false
                }
                this.startIndex = index 
                item.active  = true
                this.endIndex = ''
            }else{
                if(this.startIndex>index){
                    this.endIndex = this.startIndex
                    this.startIndex = index
                }else{
                    this.endIndex = index
                }
                for(let i = 0;i<this.timeArr.length;i++){
                    if(i>=this.startIndex&&i<=this.endIndex){
                        this.timeArr[i].active = true
                    }else{
                        this.timeArr[i].active = false
                    }
                }
            }
        },
        sureFn(){
            this.showConfirm = true
        },
        sureCheckDay(){
            this.info.sk_date = this.checkDayStr()
            this.showDayMask = false
        },
        getData(){
            let data ={
                family_course_id:this.$route.query.family_course_id,
                course_rule_name:this.$route.query.course_rule_name,
                course_price:this.$route.query.course_price,
                num:this.$route.query.num,
            }
            orderSub(data).then((res)=>{
                if(res.data.code==1){
                    this.orderInfo = res.data.data
                    // this.activeDate = res.data.data.appointment_time[0].date
                    if(res.data.data.user_order_address){
                        let address = res.data.data.user_order_address
                        this.info.service_address = address.service_address
                        this.info.house_number = address.house_number
                        this.info.user_name = address.user_name
                        this.info.user_phone = address.user_phone
                    }
                }else{
                    this.showToast = true
                    this.showText = res.data.msg
                }
                this.showCont = true
            }).catch((err)=>{
                console.log(err)
                this.showCont = true
                this.showTimeOut = true
            })
        },
      },
      mounted(){
      },
      created() {
        this.getData()
      },
    }
  </script>
  
  <style scoped lang="less">
  .container{
    width: 100%;
    background-color: #f7f8fa;
    padding-top: 1rem;
    padding-bottom: 3.75rem;
    box-sizing: border-box;
    .goodsItem{
        width: 92%;
        margin: 0 auto;
        padding: 0.75rem 4%;
        box-sizing: border-box;
        background-color: #fff;
        margin-bottom: 1rem;
        display: flex;
        border-radius: 0.25rem;
        align-items: center;
        .goodsImg{
        width: 4rem;
        height: 4rem;
        overflow: hidden;
        border-radius: 0.25rem;
        margin-right: 0.5rem;
        img{
            width: 100%;
        }
        }
        .goodsInfo{
        width: 0.5rem;
        flex: 1;
        display: flex;
        flex-direction: column;
        height: 4rem;
        justify-content: space-between;
        .goodsName{
            width: 100%;
            line-height: 1rem;
            font-size: 0.7rem;
        }
        .sk_time{
            font-size: 0.6rem;
            color: #666;
        }
        .goodsType{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .goodsNumCont{
            display: flex;
            height: 1rem;
            line-height: 1rem;
            color: #999;
            font-size: 0.6rem;
            align-items: center;
            }
            .goodsTypeItem{
            padding:0.1rem 0.5rem;
            background-color: #F6F6F6;
            font-size: 0.6rem;
            height: 1rem;
            line-height: 1rem;
            border-radius: 0.75rem;
            color: #999;
            }
        }
        .goodsInfoPrice{
            color: #999;
            display: flex;
            justify-content: space-between;
            font-size: 0.6rem;
            .sk_time{
                display: flex;
                align-items: center;
                .iconfont{
                    margin-right: 0.25rem;
                }
            }
        }
        }
    }
    .formCont{
      width: 92%;
      margin: 0 auto;
      padding: 1rem 4%;
      border-radius: 0.25rem;
      background-color: #fff;
      box-sizing: border-box;
      .formHead{
        font-size: 0.75rem;
        font-weight: bold;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.75rem;
      }
      .formList{
        width: 100%;
        .formItem{
          padding: 0.25rem 0;
          width: 100%;
          display: flex;
          position: relative;
          border-bottom: 1px solid rgb(245,245,245);
          .maskCont{
                width: 100%;
                padding: 0.75rem 4%;
                box-sizing: border-box;
                .maskName{
                    width: 100%;
                    text-align: center;
                    font-size: 0.8rem;
                    font-weight: bold;
                    margin-bottom: 0.75rem;
                }
                .timeCont{
                    width: 100%;
                    box-sizing: border-box;
                    display: flex;
                    flex-wrap: wrap;
                    max-height: 15rem;
                    overflow-y: auto;
                    box-sizing: border-box;
                    .timeItem{
                        width: 22%;
                        margin-left: 4%;
                        box-sizing: border-box;
                        text-align: center;
                        .timeItemMain{
                            border: 1px solid #666;
                            background-color: #ebebeb;
                            color: #666;
                            width: 100%;
                            padding: 0.75rem 0;
                            box-sizing: border-box;
                            border-radius: 0.25rem;
                            margin-bottom: 0.75rem;
                            position: relative;
                            overflow: hidden;
                            
                            .fullTag{
                                position: absolute;
                                right: 0;
                                top: 0;
                                padding: 0.05rem 0.15rem;
                                font-size: 0.6rem;
                                // background-color: #ebebeb;
                            }
                        }
                        .activeTimeMain{
                            background-color: #6455d4;
                            border: 1px solid #6455d4;
                            color: #fff;
                            
                        }
                    }
                    .noLeft{
                        margin-left: 0;
                    }
                }
                .setBtn{
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    .setBtnItem{
                        width: 48%;
                    }
                }
            }
          .formName{
              width: 4rem;
              font-size: 0.7rem;
              color: #646566;
              margin-right: 0.75rem;
              box-sizing: border-box;
          }
        }
        .flexCenter{
            align-items: center;
        }
        .noBorder{
          border-bottom: none;
        }
      }
    }
    .hasMargin{
        margin-top: 0.75rem;
    }
    
    .orderTip{
        width: 92%;
        margin: 0.75rem auto;
        font-size: 0.6rem;
        color: #666;
    }
    .ctrlCont{
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        background-color: #fff;
        display: flex;
        justify-content: space-between;
        padding: 0.5rem 4%;
        box-sizing: border-box;
        align-items: center;
        .leftBtn{
        }
        .rightBtn{
            font-size: 0.7rem;
            background-color: #6455d4;
            color: white;
            padding: 0.5rem 2.5rem;
            border-radius: 1rem;
            letter-spacing: 0.2rem;
        }
    }
    .orderMask{
        padding: 0.75rem;
        box-sizing: border-box;
        .maskTitle{
        font-size: 0.8rem;
        font-weight: bold;
        text-align: center;
        }
        .maskTip{
        margin-top: 0.5rem;
        line-height: 1.1rem;
        }
    }
  }
  </style>
  
